<template>
  <div>
    <el-form ref="companyRef" :inline="true" :model="companyCl.companySeachMethod">
      <el-form-item label="公司名称：" prop="name">
        <el-input v-model="companyCl.companySeachMethod.name" placeholder="公司名称" type="text"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="companyCl.companySeachMethod.phone" placeholder="电话" type="text"></el-input>
      </el-form-item>
      <el-form-item label="创建时间" prop="startEndTime">
        <el-date-picker
          v-model="companyCl.companySeachMethod.startEndTime"
          end-placeholder="结束时间"
          format="YYYY-MM-DD HH:mm:ss"
          range-separator="-"
          start-placeholder="开始时间"
          type="datetimerange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">搜索</el-button>
        <el-button type="primary" @click="resetS(companyRef)">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-row :gutter="20">
      <el-col :span="6">
        <el-button type="primary" @click="opendialog()">
          <el-icon>
            <Plus />
          </el-icon>
          添加公司
        </el-button>
      </el-col>
    </el-row>
  </div>
  <div>
    <el-table :data="companyCl.tableData" stripe style="width: 100%">
      <el-table-column align="center" label="序号" type="index" width="80">
        <template #default="scope">
          {{ (companyCl.page - 1) * companyCl.size + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="公司名称" prop="name" width="180" />
      <el-table-column align="center" header-align="center" label="电话" prop="phone" width="180" />
      <el-table-column align="center" header-align="center" label="地址" prop="address" width="180" />
      <el-table-column align="center" header-align="center" label="客户端默认" prop="defaulted">
        <template #default="scope">
          <el-switch v-model="scope.row.defaulted" :active-value="true" :inactive-value="false" active-text="是" inactive-text="否" inline-prompt @change="chengStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" header-align="center" label="公司口号" prop="slogan" width="180" />
      <el-table-column align="center" header-align="center" label="创建时间" prop="ctime" width="180" />
      <el-table-column align="center" fixed="right" header-align="center" label="操作" width="210">
        <template #default="scope">
          <el-button link size="small" type="primary" @click="edit(scope.row.id)">编辑 </el-button>
          <el-button link size="small" type="primary" @click="addPicture(scope.row.id)">添加公司图片 </el-button>
          <el-popconfirm title="确定要删除吗?" @confirm="deleteCompany(scope.row.id)">
            <template #reference>
              <el-button link type="primary">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div>
    <el-dialog v-model="companyCl.dialogFormVisible" :title="companyCl.title" destroy-on-close @close="resetE(companyEditRef)">
      <el-form ref="companyEditRef" :model="companyCl.companyEdit" :rules="companyCl.addRules">
        <el-form-item label="公司名称" prop="name">
          <el-input v-model="companyCl.companyEdit.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="公司电话" prop="phone">
          <el-input v-model="companyCl.companyEdit.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item autocomplete="off" label="创建时间" prop="cTime">
          <el-date-picker v-model="companyCl.companyEdit.cTime" format="YYYY-MM-DD" placeholder="请输入创建时间" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item autocomplete="off" label="地址" prop="allCity">
          <el-cascader v-model="companyCl.companyEdit.allCity" :options="companyCl.relationTrees" :props="companyCl.props" clearable />
        </el-form-item>
        <el-form-item autocomplete="off" label="详细地址" prop="address">
          <el-input v-model="companyCl.companyEdit.address"></el-input>
        </el-form-item>
        <el-form-item label="公司口号" prop="slogan">
          <el-input v-model="companyCl.companyEdit.slogan" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item autocomplete="off" label="简介" prop="introduction">
          <Editor v-model:editorValue="companyCl.companyEdit.introduction"></Editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="saveCompany">保存 </el-button>
          <el-button @click="companyCl.dialogFormVisible = false">取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <div>
    <el-dialog v-model="companyCl.dialogCompanyPicture" destroy-on-close title="添加图片" @close="resetP(companyPictureRef)">
      <el-form ref="companyPictureRef" :model="companyCl.companyPictureInfo" :rules="companyCl.addPictureRules">
        <el-form-item autocomplete="off" label="图片" prop="url">
          <ImgUpload v-model:file_url="companyCl.companyPictureInfo.url" :file_max="5" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="savePicture">保存 </el-button>
          <el-button @click="companyCl.dialogCompanyPicture = false">取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <Page :pageData="companyCl" @selectPage="search"></Page>
</template>
<script lang="ts" setup>
import { companyClass, companyInfo, companyPicture, companySeachInfo } from '@/page/types/admin/company'
import { addCompany, addCompanyPicture, companyDetail, companyPage, delCompany, editCompany, getCompanyPricture } from '@/api/admin'
import { ElMessage, FormInstance } from 'element-plus/es'
import Editor from '@/components/MyEditor.vue'
import Page from '@/components/Page.vue'
import { relationCity } from '@/api/system'
import { relationTreeInfo } from '@/page/types/system/city'
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import type { UploadUserFile } from 'element-plus'
import ImgUpload from '@/components/ImgsUpload.vue'
const companyCl = reactive(new companyClass())
const companyRef = ref()
const companyEditRef = ref()
const companyPictureRef = ref()
const fileList = ref<UploadUserFile[]>([])
const search = () => {
  if (companyCl.companySeachMethod.startEndTime) {
    companyCl.companySeachMethod.startTime = companyCl.companySeachMethod.startEndTime[0]
    companyCl.companySeachMethod.endTime = companyCl.companySeachMethod.startEndTime[1]
  }
  companyPage(companyCl.companySeachMethod).then(res => {
    const data = res.data.records as companySeachInfo[]
    companyCl.tableData = data
    companyCl.page = res.data.current
    companyCl.size = res.data.size
    companyCl.total = res.data.total
  })
}
const handleSizeChange = (val: number) => {
  companyCl.companySeachMethod.size = val
  search()
}
const handleCurrentChange = (val: number) => {
  companyCl.companySeachMethod.page = val
  search()
}
const resetS = (form: FormInstance | undefined) => {
  if (!form) return
  form.resetFields()
}
const resetE = (form: FormInstance | undefined) => {
  if (!form) return
  form.resetFields()
  companyCl.companyEdit = {} as companyInfo
}
const resetP = (form: FormInstance | undefined) => {
  if (!form) return
  form.resetFields()
  companyCl.companyPictureInfo = {} as companyPicture
}
const opendialog = () => {
  companyCl.dialogFormVisible = true
  const data = {} as relationTreeInfo
  relationCity(data).then(res => {
    const data = res.data as relationTreeInfo[]
    companyCl.relationTrees = data
  })
}
const saveCompany = () => {
  if (companyCl.companyEdit.allCity.length > 0) {
    companyCl.companyEdit.province = companyCl.companyEdit.allCity[0]
    companyCl.companyEdit.city = companyCl.companyEdit.allCity[1]
    companyCl.companyEdit.district = companyCl.companyEdit.allCity[2]
  }
  if (companyCl.companyEdit.id) {
    editCompany(companyCl.companyEdit).then(res => {
      companyCl.dialogFormVisible = false
      search()
      ElMessage({
        message: '更新公司信息成功',
        type: 'success'
      })
    })
  } else {
    addCompany(companyCl.companyEdit).then(res => {
      companyCl.dialogFormVisible = false
      search()
      ElMessage({
        message: '保存公司信息成功',
        type: 'success'
      })
    })
  }
}
const chengStatus = (data: any) => {
  editCompany(data).then(res => {
    companyCl.dialogFormVisible = false
    search()
    ElMessage({
      message: data.defaulted ? '启动成功' : '禁用成功',
      type: 'success'
    })
  })
}
const deleteCompany = (id: string) => {
  delCompany(id).then(res => {
    search()
    ElMessage({
      message: '删除公司信息成功',
      type: 'success'
    })
  })
}
const edit = (id: string) => {
  const data = {} as relationTreeInfo
  relationCity(data).then(res => {
    const data = res.data as relationTreeInfo[]
    companyCl.relationTrees = data
  })
  companyDetail(id).then(res => {
    companyCl.dialogFormVisible = true
    const data = res.data as companyInfo
    companyCl.companyEdit = data
    if (data.province && data.district && data.city) {
      companyCl.companyEdit.allCity = [data.province, data.city, data.district]
    } else if (data.province && data.city && !data.district) {
      companyCl.companyEdit.allCity = [data.province, data.city]
    } else if (data.province && !data.city && !data.district) {
      companyCl.companyEdit.allCity = [data.province]
    }
  })
}
const addPicture = (id: string) => {
  companyCl.companyPictureInfo.id = id
  getCompanyPricture(id).then(res => {
    const urlList = res.data as string[]
    if (urlList.length > 0) {
      companyCl.companyPictureInfo.url = urlList
    }
    companyCl.dialogCompanyPicture = true
  })
}

const savePicture = () => {
  addCompanyPicture(companyCl.companyPictureInfo).then(res => {
    companyCl.dialogCompanyPicture = false
    ElMessage({
      message: '添加图片成功',
      type: 'success'
    })
  })
}
onMounted(() => {
  search()
})
</script>
<style lang="scss" scoped>
.pagination-block {
  position: relative;

  .el-pagination {
    margin-top: 20px;
    position: absolute;
    right: 0%;
  }
}
.el-select {
  width: 200px;
}
</style>
